package ex.my_map;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.function.BiFunction;
import java.util.function.Function;

public class MyTry {
    public static void main(String[] args) {
        Map.Entry<String,Double> cpu      = Map.entry("锐龙CPU",2999.00);
        Map.Entry<String,Double> board    = Map.entry("华硕主板",1699.00);
        Map.Entry<String,Double> graphics = Map.entry("微星魔龙显卡",9999.00);
        Map.Entry<String,Double> sticks   = Map.entry("金士顿64G内存条",2399.00);
        Map.Entry<String,Double> disk     = Map.entry("西部数据SSD硬盘",1299.00);
        Map.Entry<String,Double> chassis  = Map.entry("追风者机箱",899.00);
        Map.Entry<String,Double> supply   = Map.entry("振华电源",1239.00);
        Map.Entry<String,Double> water    = Map.entry("华硕水冷散热器",1699.00);
        Map<String,Double> total = Map.ofEntries(cpu , board , graphics , sticks , disk , chassis , supply , water);
        Map<String,Double> map = new HashMap<>(total);
        Random random = new Random();
        System.out.println(map);
        map.compute("装机预算", new BiFunction<String, Double, Double>() {
            @Override
            public Double apply(String s, Double aDouble) {
                System.out.println(s+":"+aDouble);
                return random.nextDouble(500);
            }
        });
        System.out.println(map);
        System.out.println("--------------------------------------------------------");
        map.computeIfAbsent("邮费", s ->random.nextDouble(10));
        System.out.println(map);
        map.putIfAbsent("锐龙CPU", 3000.0);
        System.out.println(map.putIfAbsent("锐龙CPU", 524452452.0));
        System.out.println(map);
        System.out.println("--------------------------------------------------------");
        map.computeIfPresent("锐龙CPU",(x,y)->y/2);
        System.out.println(map);
        System.out.println("--------------------------------------------------------");
        map.forEach((x,y)->System.out.println(x+":"+y));
        System.out.println("--------------------------------------------------------");
        System.out.println(map);
        map.put("型号",null);
        map.merge("型号", 2999.0, new BiFunction<Double, Double, Double>() {
            @Override
            public Double apply(Double aDouble, Double aDouble2) {
                System.out.println(aDouble);
                System.out.println(aDouble2);
                return null;
            }
        });
        System.out.println(map);

    }
}
